package com.wly.mina;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.logging.Logger;

import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import com.wly.mina.filter.GPBProtocolFactory;
import com.wly.mina.filter.handle.GameServerHandle;

public class MinaMain
{
	private final int      PORT = 1234;

	private static MinaMain _instance;
	private NioSocketAcceptor _accept;
	//private Logger log = LoggerFactory.getLogger("MinaMain");
	
	private Logger log = Logger.getLogger("MinaMain");
	private void MinaMain()
	{
	}
	
	static public MinaMain getInstance()
	{
		if(_instance == null)
		{
			_instance = new MinaMain();
		}
		return _instance;
	}
	
	public void initMina() throws Exception
	{
		_accept = new NioSocketAcceptor();
		_accept.getFilterChain().addLast("log", new LoggingFilter());
		_accept.getFilterChain().addLast("codec", new ProtocolCodecFilter(new GPBProtocolFactory()));
		_accept.setHandler(new GameServerHandle());
		_accept.bind(new InetSocketAddress(PORT));
		log.info("bind socket on port: "+PORT);
	}
}
